using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace SeguradoraIA.Framework.Log
{
    public abstract class LogData
    {
        protected static string TrataParametros(object[] parametros)
        {
            List<string> parametrosTratados = new List<string>();
            string aux;
            foreach (object objeto in parametros)
            {
                if (objeto == null)
                {
                    aux = "null";
                }
                else
                {
                    if (objeto is IEnumerable && !(objeto is string))
                    {

                        StringBuilder sb = new StringBuilder();
                        foreach (object o in (IEnumerable)objeto)
                        {
                            sb.AppendLine(GetObjectValues(o));
                        }
                        aux = sb.ToString();
                    }
                    else
                    {
                        aux = GetObjectValues(objeto);
                    }
                }
                parametrosTratados.Add(aux);
            }

            return string.Join(",", parametrosTratados.ToArray());
        }
        protected static string GetObjectValues(object obj)
        {
            if (obj.GetType().IsPrimitive || obj is string || obj is decimal)
            {
                return obj.ToString();
            }

            ObjectStructure objectStructure = ObjectStructure.GetObjectStructure(obj);
            return objectStructure.ToString();
        }


        protected LogData()
        {
            CreationDate = DateTime.Now;
            ContextThread = Thread.CurrentThread;
            StackTracer.GetStackInfo(this);
        }

        public string Message;
        public string Information;
        public readonly DateTime CreationDate;
        public readonly Thread ContextThread;
        public string Line;
        public string Method;
        public string ParametersDeclaration;
        public string FileName;

        public bool ShouldLog(bool logMessage, bool logReturn, bool logSQL, bool logEntry, bool logException)
        {

            return (this is SimpleLogData && logMessage) ||
                   (this is ReturnLogData && logReturn) ||
                   (this is SQLLogData && logSQL) ||
                   (this is EntryLogData && logEntry) ||
                   (this is ExceptionLogData && logException);
        }
    }
}